﻿using FastReport.Barcode;
using Humanizer;
using NPOI.HSSF.Record.Chart;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Web;


namespace LanDBom.Models.Contracts
{
    public class Find_Contracts
    {
        LDbaseEntities LD = new LDbaseEntities();
        public int find_Contracts_with_D_Ton(string D_Ton)
        {
            int index = LD.Designdraw.Where(x => x.D_Ton == D_Ton).Count();
            return index;
        }

        public List<Designdraw> find_Contra(int Namess, int JIN)
        {
            var denglu = LD.Dengli.Where(s => s.DE_ID == Namess).FirstOrDefault();
            var AppLO = LD.Applications.Where(s => s.APP_she != "N" && (s.AP_QTJY == "" || s.AP_QTJY == null)).ToList();
            List<Designdraw> index = new List<Designdraw>();
            foreach (var item in AppLO)
            {
                var A = LD.Designdraw.Where(s => s.D_Ton == item.AP_bian).FirstOrDefault();
                if (A != null)
                {
                    index.Add(A);
                }
            }
            if (denglu.DE_zhiji > 1)
            {
                index = LD.Designdraw.ToList();
            }
            else
            {
                if (denglu.DE_bumeng == 2)
                {
                    var dpp = LD.ShejinBeiZhu.Where(s => s.SJR_id == Namess).ToList();
                    if (JIN == 2)
                    {
                        dpp = dpp.Where(s => s.SJ_ZT != "完成").ToList();
                    }
                    else
                    {
                        dpp = dpp.Where(s => s.SJ_ZT == "完成").ToList();
                    }
                    var shun = new List<Designdraw>();
                    foreach (var item in dpp)
                    {
                        var bian = item.SJ_Bian;
                        var A = LD.Designdraw.FirstOrDefault(s => s.D_Ton == bian);
                        if (A != null)
                        {
                            shun.Add(A);
                        }
                    }
                    index = shun;
                }
                else
                {
                    var sji = denglu.DE_nae;
                    var dpp = LD.Applications.Where(s => s.AP_ren == sji).ToList();
                    var shun = new List<Designdraw>();
                    foreach (var item in dpp)
                    {
                        var bian = item.AP_bian;
                        var A = LD.Designdraw.FirstOrDefault(s => s.D_Ton == bian);
                        if (A != null)
                        {
                            shun.Add(A);
                        }
                    }
                    index = shun;
                }
            }
            return index;
        }

        public List<Designdraw> find_Contracts_bao(string BB_SQ_xmmc, string BB_SQ_khmc, string BB_SQ_gjz, string jssj, string qssj, string danhao, string xmlx, int Namess, int JIN)
        {
            List<Designdraw> index = find_Contra(Namess, JIN);

            foreach (var i in index)
            {

                i.D_Applic = i.D_Applic == null ? "1/11/1111 12:00:00 AM" : i.D_Applic;
                i.D_reDate = i.D_reDate == null ? "1/11/1111 12:00:00 AM" : i.D_reDate;
                i.D_PN = i.D_PN == null ? "1/11/1111 12:00:00 AM" : i.D_PN;
                if (i.D_CN == null)
                {
                    i.D_CN = "";
                }
                if (i.D_Ton == null)
                {
                    i.D_Ton = "";
                }
                var app = LD.Applications.Where(x => x.AP_bian == i.D_Ton).FirstOrDefault();
                i.D_kong = app?.App_xmzt;
                i.D_SR = string.Join(",",LD.ShejinBeiZhu.Where(s => s.SJ_Bian == i.D_Ton)
                                          .Select(item => LD.Dengli.FirstOrDefault(d => d.DE_ID == item.SJR_id).DE_nae)
                                          .Where(name => name != ""));
            }
            var query = index.AsQueryable();

            if (!string.IsNullOrEmpty(danhao))
                query = query.Where(x => x.D_Ton.Contains(danhao));

            if (!string.IsNullOrEmpty(BB_SQ_xmmc))
                query = query.Where(x => x.D_PN.Contains(BB_SQ_xmmc));

            if (!string.IsNullOrEmpty(BB_SQ_khmc))
                query = query.Where(x => x.D_CN.Contains(BB_SQ_khmc));

            if (!string.IsNullOrEmpty(BB_SQ_gjz))
                query = query.Where(x =>
                    x.D_Ton.Contains(BB_SQ_gjz) ||
                    x.D_PN.Contains(BB_SQ_gjz) ||
                    x.D_CN.Contains(BB_SQ_gjz) ||
                    x.D_Applic.Contains(BB_SQ_gjz) ||
                    x.D_reDate.Contains(BB_SQ_gjz));

            if (DateTime.TryParse(jssj, out var endDate))
                query = query.Where(x => DateTime.Parse(x.D_Applic) <= endDate);

            if (DateTime.TryParse(qssj, out var startDate))
                query = query.Where(x => DateTime.Parse(x.D_Applic) >= startDate);

            if (!string.IsNullOrEmpty(xmlx) && xmlx != "0")
            {
                switch (xmlx)
                {
                    case "WON Opportunities":
                        query = query.Where(x => x.D_kong == "WON Opportunities");
                        break;
                    case "Open Opportunities":
                        query = query.Where(x => x.D_kong == "Open Opportunities");
                        break;
                    case "LOST Opportunities":
                        query = query.Where(x => x.D_kong == "LOST Opportunities");
                        break;
                    default:
                        break;
                }
            }
            index = query.OrderByDescending(x => DateTime.Parse(x.D_Applic)).ToList();
            //判  JL是发货的值
            var denglu = LD.Dengli.Where(s => s.DE_ID == Namess).FirstOrDefault();
            foreach (var item in index)
            {
                if (denglu.DE_bumeng == 2)
                {
                    if (denglu.DE_zhiji == 1)
                    {
                        var sjbz = LD.ShejinBeiZhu.FirstOrDefault(x => x.SJ_Bian == item.D_Ton && x.SJR_id == denglu.DE_ID);
                        item.D_JL = sjbz?.SJ_ZT == "完成" ? "完成递交，等待审核" : "等待递交";
                    }
                    else
                    {
                        switch (item.D_zhuang)
                        {
                            case "2":
                                item.D_JL = "等待设计主管分配";
                                break;
                            case "3":
                                item.D_JL = GetDesignProgress(item.D_Ton);
                                break;
                            default:
                                break;
                        }
                    }
                }
                else
                {
                    switch (item.D_zhuang)
                    {
                        case "1":
                            item.D_JL = "等待提交到设计部";
                            break;
                        case "2":
                            item.D_JL = "等待设计主管分配任务";
                            break;
                        case "3":
                            item.D_JL = "等待设计人员递交";
                            break;
                        case "4":
                            item.D_JL = "等待业务人员审核";
                            break;
                        case "5":
                            item.D_JL = "设计任务已完成";
                            break;
                        default:
                            break;
                    }
                }
            }
            return index;

            string GetDesignProgress(string ton)
            {
                int total = LD.ShejinBeiZhu.Count(x => x.SJ_Bian == ton);
                int completed = LD.ShejinBeiZhu.Count(x => x.SJ_Bian == ton && x.SJ_ZT == "完成");
                return completed > 0 ? $"{completed}/{total}设计人员已递交" : "设计人员设计中";
            }
        }
        public List<Designdraw> find_Contracts_bao(int D_Id)
        {
            List<Designdraw> DesList = LD.Designdraw.Where(x => x.D_id == D_Id).ToList();
            return DesList;
        }
        public List<Designdraw> find_Contracts_ti(int D_Id, string D_Applic, string D_Ton)
        {
            List<Designdraw> DesList = LD.Designdraw.Where(x => x.D_zhuang == "提交").ToList();
            if (D_Id != -99)
            {
                DesList = DesList.Where(x => x.D_id == D_Id).ToList();
            }
            if (D_Applic != "")
            {
                DesList = DesList.Where(x => x.D_Applic.Contains(D_Applic)).ToList();
            }
            if (D_Ton != "")
            {
                DesList = DesList.Where(x => x.D_Ton.Contains(D_Ton)).ToList();
            }
            return DesList;
        }
        public List<BeiZhu> find_bz(string D_Ton)
        {
            List<BeiZhu> beiZhus = LD.BeiZhu.Where(x => x.B_Bian == D_Ton).ToList();
            return beiZhus;
        }
        public List<Designdraw> find_Contracts(string D_Ton)
        {
            List<Designdraw> index = LD.Designdraw.Where(x => x.D_Ton == D_Ton).ToList();
            return index;
        }
        public List<Designdraw> find_Contracts(int BB_WT_id, string BB_WT_sqdh, string BB_WT_dj, string BB_WT_zt, string BB_WT_xmmc, string cong, string dao)
        {
            List<Designdraw> index = LD.Designdraw.ToList();
            List<Designdraw> des = new List<Designdraw>();

            foreach (var i in index)
            {
                if (i.D_Ton == null)
                {
                    i.D_Ton = "";
                }
                if (i.D_Applic == null)
                {
                    i.D_Applic = "1/11/1111 12:00:00 AM";
                }
                if (i.D_App == null)
                {
                    i.D_App = "";
                }
                if (i.D_REC == null)
                {
                    i.D_REC = "";
                }
                if (i.D_LV == null)
                {
                    i.D_LV = "";
                }
                if (i.D_zhuang == null)
                {
                    i.D_zhuang = "";
                }
                if (i.D_PN == null)
                {
                    i.D_PN = "";
                }

            }
            if (BB_WT_id != -99)
            {
                index = index.Where(x => x.D_id == BB_WT_id).ToList();
            }
            if (BB_WT_sqdh != "")
            {
                index = index.Where(x => x.D_Ton.Contains(BB_WT_sqdh)).ToList();
            }
            if (BB_WT_dj != "")
            {
                index = index.Where(x => x.D_LV.Contains(BB_WT_dj)).ToList();
            }
            if (BB_WT_zt != "")
            {
                if (BB_WT_zt == "1")
                {
                    index = index.Where(x => x.D_zhuang == "5").ToList();
                }
                else if (BB_WT_zt == "3")
                {
                    index = index.Where(x => x.D_zhuang == "-99").ToList();
                }
                else if (BB_WT_zt == "2")
                {
                    index = index.Where(x => x.D_zhuang != "5" && x.D_zhuang != "-99").ToList();
                }

            }
            if (BB_WT_xmmc != "")
            {
                index = index.Where(x => x.D_PN.Contains(BB_WT_xmmc)).ToList();
            }
            if (cong != "")
            {

                DateTime result = DateTime.Parse(cong);
                index = index.Where(x => DateTime.Parse(x.D_Applic) >= result).ToList();
            }
            if (dao != "")
            {
                DateTime result = DateTime.Parse(dao);
                index = index.Where(x => DateTime.Parse(x.D_Applic) <= result).ToList();
            }
            foreach (Designdraw d in index)
            {
                Applications applications = LD.Applications.Where(x => x.AP_bian == d.D_Ton).FirstOrDefault();
                if (applications != null)
                {
                    if (applications.App_bei == "提交")
                    {
                        des.Add(d);
                    }
                }
            }
            des = des.OrderByDescending(s => s.D_Applic).ToList();
            return des;
        }
        public List<Clirntele> find_Client_name(string name)
        {
            List<Clirntele> clirnteles = LD.Clirntele.Where(x => x.CL_name.Contains(name)).ToList();
            return clirnteles;
        }

        public List<Design> find_Design(string DTon)
        {
            List<Design> listdes = LD.Design.Where(x => x.DE_shen == DTon).ToList();
            return listdes;
        }
        public List<Design> find_Design_liy(int DTon)
        {
            List<Design> listdes = LD.Design.Where(x => x.DE_id == DTon).ToList();
            return listdes;
        }
        public int find_Design(int? bOM2_id)
        {
            int index = LD.Design.Where(x => x.BOM2_id == bOM2_id).Count();
            return index;
        }

        public List<Design> find_Design()
        {
            List<Design> index = LD.Design.ToList();
            return index;
        }
        public List<BOMini> find_Boms(string APP_bian)
        {
            List<BOMini> boms = LD.BOMini.Where(x => x.BOM2_bian == APP_bian).ToList();
            return boms;

        }
        public int find_Design(int id)
        {
            Design index = LD.Design.Where(x => x.DE_id == id).FirstOrDefault();
            LD.Design.Remove(index);
            return LD.SaveChanges();
        }

        public string fing_bushu(string bian)
        {
            Applications index = LD.Applications.Where(x => x.AP_bian == bian).First();
            return index.App_zhand;
        }

        public List<ShejinBeiZhu> find_SJBZ(string APP_bian, int id)
        {
            List<ShejinBeiZhu> boms = LD.ShejinBeiZhu.Where(x => x.SJ_Bian == APP_bian && x.SJR_id == id).ToList();
            return boms;
        }

        public List<Design> find_Con(string bian, int hydl, int hyxl, string bzpl, string tzlb, string bzwmc, string Gunaj)
        {
            var des = LD.Design.Where(x => x.DE_tuiLei == "通过").ToList();
            foreach (Design d in des)
            {
                if (d.DE_shen == null)
                {
                    d.DE_shen = "";
                }
                else if (d.DE_bzpl == null)
                {
                    d.DE_bzpl = "";
                }
                else if (d.DE_bzw == null)
                {
                    d.DE_bzw = "";
                }
                else if (d.DE_tzlb == null)
                {
                    d.DE_bzpl = "";

                }
            }
            if (bian != "")
            {
                des = des.Where(x => x.DE_shen.Contains(bian)).ToList();
            }
            if (hydl != 0)
            {
                des = des.Where(x => x.DE_h1_id == hydl).ToList();
            }
            if (hyxl != 0)
            {
                des = des.Where(x => x.DE_h2_id == hyxl).ToList();
            }
            if (bzpl != "null")
            {
                des = des.Where(x => x.DE_bzpl.Contains(bzpl)).ToList();
            }
            if (tzlb != "null")
            {
                des = des.Where(x => x.DE_tzlb.Contains(tzlb)).ToList();
            }
            if (bzwmc != "")
            {
                des = des.Where(x => x.DE_bzw.Contains(bzwmc)).ToList();
            }
            if (Gunaj != "")
            {
                des = des.Where(s => s.DE_h1_id == null || s.IndustryCategory.ic_name.Contains(Gunaj)).ToList();
                des = des.Where(s => s.DE_h2_id == null || s.IndustrySubcategory.isu_name.Contains(Gunaj)).ToList();
                des = des.Where(x => x.DE_shen.Contains(Gunaj) || x.DE_bzpl.Contains(Gunaj) || x.DE_tzlb.Contains(Gunaj) || x.DE_bzw.Contains(Gunaj) || x.De_ren.Contains(Gunaj) || x.DE_TZname.Contains(Gunaj)).ToList();
            }
            return des;
        }

    }
}